package com.fang.transfer.file.client.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class UnitConversionUtil {


    /**
     * 单位转换
     *
     * @param size
     * @return
     */
    public static String getPrintSize(long size) {
        if (size < 1024) {
            return size + "B";
        }
        BigDecimal result = toNextUtilValue(new BigDecimal(size));
        if (result.longValue() < 1024) {
            return result.toPlainString() + "KB";
        }
        result = toNextUtilValue(result);
        if (result.longValue() < 1024) {
            return result.toPlainString() + "MB";
        }
        result = toNextUtilValue(result);
        return result.toPlainString() + "GB";
    }

    private static BigDecimal toNextUtilValue(BigDecimal size) {
        BigDecimal divide = size.divide(new BigDecimal(1024), 2, RoundingMode.UP);
        return divide;
    }


}
